package fichier;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


@SuppressWarnings("serial")
public class AjouterClient extends JFrame implements ActionListener, KeyListener {

	public String nom;
	private String adresse1;
	private String adresse2;
	private String CP;
	private String ville;
	@SuppressWarnings("unused")
	private String pays;
	private JPanel pane;
	private JButton okBut;
	private JMenuBar menuBar;
	private JMenu fichier;
	private JMenu mModele;
	private JMenu mClient;
	private JMenuItem deconnexion;
	private JMenuItem miRechercherM;
	private JMenuItem miAjouterM;
	private JMenuItem miModifierM;
	private JMenuItem miRechercherC;
	private JMenuItem miAjouterC;
	private JMenuItem miModifierC;
	private JMenuItem miModifierPwd;
	private JMenuItem miAjoutBox;
	private JLabel nomLab;
	private JLabel adresse1Lab;
	private JLabel adresse2Lab;
	private JLabel cpLab;
	private JLabel villeLab;
	private JLabel paysLab;
	private JTextField paysTxt;
	private JTextField nomTxt;
	private JTextField adresse1Txt;
	private JTextField adresse2Txt;
	private JTextField cpTxt;
	private JTextField villeTxt;


	public String getNom() {
		return nom;
	}



	public void setNom(String nom) {
		this.nom = nom;
	}



	public String getAdresse1() {
		return adresse1;
	}



	public void setAdresse1(String adresse1) {
		this.adresse1 = adresse1;
	}



	public String getAdresse2() {
		return adresse2;
	}



	public void setAdresse2(String adresse2) {
		this.adresse2 = adresse2;
	}



	public String getCP() {
		return CP;
	}



	public void setCP(String cP) {
		CP = cP;
	}



	public String getVille() {
		return ville;
	}



	public void setVille(String ville) {
		this.ville = ville;
	}



	public JPanel getPane() {
		return pane;
	}



	public void setPane(JPanel pane) {
		this.pane = pane;
	}



	public JButton getOkBut() {
		return okBut;
	}



	public void setOkBut(JButton okBut) {
		this.okBut = okBut;
	}



	public void setMenuBar(JMenuBar menuBar) {
		this.menuBar = menuBar;
	}



	public JMenu getmModele() {
		return mModele;
	}



	public void setmModele(JMenu mModele) {
		this.mModele = mModele;
	}



	public JMenu getmClient() {
		return mClient;
	}



	public void setmClient(JMenu mClient) {
		this.mClient = mClient;
	}



	public JMenuItem getMiRechercherM() {
		return miRechercherM;
	}



	public void setMiRechercherM(JMenuItem miRechercherM) {
		this.miRechercherM = miRechercherM;
	}



	public JMenuItem getMiAjouterM() {
		return miAjouterM;
	}



	public void setMiAjouterM(JMenuItem miAjouterM) {
		this.miAjouterM = miAjouterM;
	}



	public JMenuItem getMiModifierM() {
		return miModifierM;
	}



	public void setMiModifierM(JMenuItem miModifierM) {
		this.miModifierM = miModifierM;
	}



	public JMenuItem getMiRechercherC() {
		return miRechercherC;
	}



	public void setMiRechercherC(JMenuItem miRechercherC) {
		this.miRechercherC = miRechercherC;
	}



	public JMenuItem getMiAjouterC() {
		return miAjouterC;
	}



	public void setMiAjouterC(JMenuItem miAjouterC) {
		this.miAjouterC = miAjouterC;
	}



	public JMenuItem getMiModifierC() {
		return miModifierC;
	}



	public void setMiModifierC(JMenuItem miModifierC) {
		this.miModifierC = miModifierC;
	}



	public AjouterClient() {
		super("Gestion des modèles - AFP Entreprises");
		this.setBounds(100,100,515,365 );
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


		this.menuBar = new JMenuBar();
		this.fichier = new JMenu("Fichier");
		this.mModele = new JMenu("Modèle");
		this.mClient = new JMenu("Client");
		this.deconnexion = new JMenuItem("Quitter");
		this.miRechercherM = new JMenuItem("Rechercher");
		this.miAjouterM = new JMenuItem("Ajouter");
		//this.setMiModifierM(new JMenuItem("Modifier"));
		this.miAjouterC = new JMenuItem("Ajouter");
		this.miModifierPwd = new JMenuItem("Modifier mot de passe");
		this.miAjoutBox = new JMenuItem("Ajouter un nouveau Box");

		this.menuBar.add(this.fichier);
		this.menuBar.add(this.mModele);
		this.menuBar.add(this.mClient);
		this.fichier.add(this.miAjoutBox);
		this.fichier.add(this.miModifierPwd);
		this.fichier.add(this.deconnexion);
		this.mModele.add(this.miRechercherM);
		this.mModele.add(this.miAjouterM);
		this.mClient.add(this.miAjouterC);
		this.setJMenuBar(this.menuBar);

		this.nom = new String ("");
		this.adresse1 = new String ("");
		this.adresse2 = new String ("");
		this.CP = new String("");
		this.ville = new String("");
		this.pane = new JPanel();
		this.okBut = new JButton("Ajouter");
		this.nomLab = new JLabel ("Nom Client :");
		this.adresse1Lab = new JLabel ("Adresse :");
		this.adresse2Lab = new JLabel ("Complément d'adresse :");
		this.cpLab = new JLabel ("Code Postal :");
		this.villeLab = new JLabel ("Ville :");
		this.nomTxt = new JTextField ("");
		this.adresse1Txt = new JTextField ("");
		this.adresse2Txt = new JTextField ("");
		this.cpTxt = new JTextField ("");
		this.villeTxt = new JTextField ("");
		this.paysLab = new JLabel("Pays");
		this.paysTxt = new JTextField("");


		

		this.getContentPane().add(this.pane);
		this.pane.add(this.nomLab);
		this.pane.add(this.adresse1Lab);
		this.pane.add(this.adresse2Lab);
		this.pane.add(this.cpLab);
		this.pane.add(this.villeLab);
		this.pane.add(this.paysLab);
		this.pane.add(this.nomTxt);
		this.pane.add(this.adresse1Txt);
		this.pane.add(this.adresse2Txt);
		this.pane.add(this.cpTxt);
		this.pane.add(this.villeTxt);
		this.pane.add(this.paysTxt);
		this.pane.add(this.okBut);
		
		this.miAjoutBox.addActionListener(this);
		this.miModifierPwd.addActionListener(this);
		this.okBut.addActionListener(this);
		this.miAjouterC.addActionListener(this);
		this.miRechercherM.addActionListener(this);
		this.miAjouterM.addActionListener(this);
		this.deconnexion.addActionListener(this);
		
		this.okBut.addKeyListener(this);
		this.nomTxt.addKeyListener(this);
		this.adresse1Txt.addKeyListener(this);
		this.adresse2Txt.addKeyListener(this);
		this.cpTxt.addKeyListener(this);
		this.villeTxt.addKeyListener(this);
		this.paysTxt.addKeyListener(this);
		
		this.pane.setBorder(BorderFactory.createTitledBorder("Ajout Client"));


		this.getContentPane().setLayout(null);
		this.pane.setLayout(null);

		this.pane.setBounds(5,5,490,295);
		this.nomLab.setBounds(30,20,150,20);
		this.adresse1Lab.setBounds(30,60,150,20);
		this.adresse2Lab.setBounds(30,100,160,20);
		this.cpLab.setBounds(30,140,120,20);
		this.villeLab.setBounds(30,180,150,20);
		this.paysLab.setBounds(30,220,150,20);
		this.nomTxt.setBounds(250,20,150,20);
		this.adresse1Txt.setBounds(250,60,150,20);
		this.adresse2Txt.setBounds(250,100,150,20);
		this.cpTxt.setBounds(250,140,80,20);
		this.villeTxt.setBounds(250,180,150,20);
		this.paysTxt.setBounds(250,220,150,20);
		this.okBut.setBounds(180,260,110,20);
		
		this.paysLab.setEnabled(true);
		this.paysTxt.setEnabled(true);

		this.setResizable(false);
		this.setVisible(true);

	}
	
	public void connec(){
		this.nom = this.nomTxt.getText();
		this.adresse1 = this.adresse1Txt.getText();
		this.adresse2 = this.adresse2Txt.getText();
		this.CP = this.cpTxt.getText();
		this.ville = this.villeTxt.getText();
		this.pays = this.paysTxt.getText();
		if(this.nom.length() == 0 || this.adresse1.length() == 0 || this.ville.length() == 0 || this.pays.length() == 0 ){
			String warning = new String(" ");
			String debut = "Le champ";
			String fin = "\nest vide";
			int nb = 0;
			if(this.nom.length() == 0){
				warning += "\nnom,";
				nb ++;
			}
			if(this.adresse1.length() == 0){
				warning += " adresse,";
				nb ++;
			}
			//if(this.CP.length() == 0){
				//warning += " code postal,";
				//nb ++;
			//}
			if(this.ville.length() == 0){
				warning += " ville,";
				nb ++;
			}	
			if(this.pays.length() == 0){
				warning += " pays,";
				nb ++;
			}	
			if(nb>1){
				fin = "\nsont vides";
				debut = "Les champs";
			}
			String message = debut + warning + fin;
			JOptionPane.showMessageDialog(this,message,"Attention !",1, new ImageIcon(getClass().getResource("/icon/warn.png")));
		}
		else if(this.nom.length() != 0 && this.adresse1.length() != 0 && this.ville.length() != 0){
			@SuppressWarnings("unused")
			//Ne pas oublier d'ajouter le pays à la fin
			Client c = new Client(nom, adresse1, adresse2, CP, ville,pays);
			if(main.testUpdate){
				JOptionPane.showMessageDialog(this,"Le client a bien été ajouté !", "Model Manager",1, new ImageIcon(getClass().getResource("/icon/ok.png")));
				this.nomTxt.setText("");
				this.adresse1Txt.setText("");
				this.adresse2Txt.setText("");
				this.cpTxt.setText("");
				this.villeTxt.setText("");
				this.paysTxt.setText("");
			} 
			else{
				JOptionPane.showMessageDialog(this,"Le client n'a pas pu être ajouté !", "Attention", 1,new ImageIcon(getClass().getResource("/icon/warn.png")));
			}
		}
	}
	public void keyPressed(KeyEvent evt) {

		if ( evt.getKeyCode() == KeyEvent.VK_ENTER)
		{
			this.connec();
		}
	}
	public void keyTyped(KeyEvent evt) { }

	public void keyReleased(KeyEvent evt) {}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == this.okBut){
			this.connec();
		}
		else if(e.getSource() == this.miRechercherM){
			this.dispose();
			//this.setVisible(false);
			@SuppressWarnings("unused")
			RechercheModele m = new RechercheModele();
		}
		else if(e.getSource() == this.miAjouterM){
			this.dispose();
			//this.setVisible(false);
			@SuppressWarnings("unused")
			AjouterModele maFenetre = new AjouterModele();
		}
		else if(e.getSource() == this.miAjouterC){
			JOptionPane.showMessageDialog(this, "Vous êtes déjà sur le formulaire d'ajout de client !","Attention !",1, new ImageIcon(getClass().getResource("/icon/warn.png")));
		}
		else if(e.getSource() == this.miModifierPwd){
			@SuppressWarnings("unused")
			ChangeMdP password = new ChangeMdP(this, true);
		}
		else if(e.getSource() == this.miAjoutBox){
			@SuppressWarnings("unused")
			AjoutBox box = new AjoutBox(this, true);
		}
		else if(e.getSource() == this.deconnexion){
			System.exit(0);	
		}
	}

}
